package com.chenxingnet.tools;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.UUID;

import javax.imageio.stream.FileImageOutputStream;

import org.springframework.web.multipart.MultipartFile;

import com.chenxingnet.common.CommonString;


public class FileUpLoadTool {

	
	
	
	
	/*
	 * // base64字符串转化成图片
    public static boolean GenerateImage(String imgFilePath,String imgBase64) { // 对字节数组字符串进行Base64解码并生成图片
        if (imgBase64 == null){
            return false;// 图像数据为空
        }

        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgBase64);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }

            // 生成jpeg图片
            ByteArrayInputStream bi = new ByteArrayInputStream(b);
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            System.out.print(imgFilePath);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
	 * 
	 */
	
	
	
	
	/*
	 *存储用户说说图片，争取写成存储图片通用方法（存储图片或者视频）
	 *参数： userid  用户id
	 *   type  文件后缀名
	 */
	public  static  String  saveImg(MultipartFile   m,String   username,String   type,int index)
	{
		String   filedir=  "";
		 String  returnDir="";
		FileImageOutputStream imageOutput =null;
		try {
			byte[] bytes = m.getBytes();
			/*
			 * 思路
			 * 1，创建文件夹
			 * 2，存储图片写入硬盘
			 * 3，返回路径
			 * */
			 filedir= FileUpLoadTool.getProjectFile();
			 filedir=filedir.replace("bin","");
			 filedir=filedir+"\\webapps\\MyCamera";
			 String  destDirName=filedir+"\\MyCameraFile\\";
			 Calendar now = Calendar.getInstance();  
			 int   month=now.get(Calendar.MONTH)+1;
		     String   datePath="\\"+now.get(Calendar.YEAR)+"\\"+month+"\\"+now.get(Calendar.DAY_OF_MONTH)+"\\";  
			 CreateFileUtil.createDir(destDirName+datePath);
			 UUID uuid = UUID.randomUUID();
			 filedir= filedir+"\\MyCameraFile\\"+datePath+username+"_"+uuid+"_"+index+"."+type;
			 imageOutput = new FileImageOutputStream(new File(filedir));  
			 imageOutput.write(bytes, 0, bytes.length);  
			 imageOutput.close();
			 returnDir=CommonString.httpHeadName+"/"+CommonString.projectName+"/"+CommonString.userTalkImg+"/"+now.get(Calendar.YEAR)+"/"+month+"/"+now.get(Calendar.DAY_OF_MONTH)+"/"+username+"_"+uuid+"_"+index+"."+type;
			 new  ImageUtil().thumbnailImage(filedir, 200, 200);//生成缩略图
		} catch (IOException e) {
			e.printStackTrace();}
		return   returnDir;
	}
	

	//获取本项目的根目录文件夹
	public static String getProjectFile() {
		   File directory = new File("");// 参数为空
	        String courseFile=null;
			try {
				courseFile = directory.getCanonicalPath();
			} catch (IOException e) {
				e.printStackTrace();
			}
	        return courseFile;
	}
	

}
